package com.dk.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.dk.excel.ExcelStyleUtil;
import com.dk.excel.MsgWork;
import com.dk.mapper.WorkMapper;
import com.dk.model.Work;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("easypoiSingleExcelViewTest")
public class EasypoiSingleExcelViewTest {

    @Autowired
    private WorkMapper workMapper;

    @GetMapping("download")
    public String download(ModelMap map) {
        List<MsgWork> list = new ArrayList<>();
        List<Work> workList = workMapper.selectList(null);
        for (Work work : workList) {
            MsgWork msgWork = new MsgWork();
            BeanUtils.copyProperties(work, msgWork);
            list.add(msgWork);
        }
        ExportParams params = new ExportParams(null, "工单数据", ExcelType.XSSF);
        params.setFreezeCol(1);//冻结列
        params.setStyle(ExcelStyleUtil.class);//自定义样式
        map.put(NormalExcelConstants.DATA_LIST, list);
        map.put(NormalExcelConstants.FILE_NAME, "工单" + new Date().getTime());//设置excel文件名称
        map.put(NormalExcelConstants.CLASS, MsgWork.class);
        map.put(NormalExcelConstants.PARAMS, params);
        return NormalExcelConstants.EASYPOI_EXCEL_VIEW;
    }
}